//
//  WCBookmark.m
//  WabbitCode
//
//  Created by William Towe on 4/12/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCBookmark.h"


#define kWCBTextFileKey @"textFile"
#define kWCBLineNumberKey @"lineNumber"

@implementation WCBookmark
- (void)dealloc {
	[_textFile release];
	[super dealloc];
}

- (BOOL)isLeaf {
	return YES;
}

- (NSString *)name {
	if (!_name) {
		_name = [[NSString stringWithFormat:@"%@:%u",[[self textFile] name],[self lineNumber]+1] retain];
	}
	return _name;
}

- (NSImage *)icon {
	return [NSImage imageNamed:@"Bookmark16x16"];
}

- (BOOL)canGetInfo {
	return NO;
}

- (id)initWithCoder:(NSCoder *)coder {
	if (!(self = [super initWithCoder:coder]))
		return nil;
	
	_textFile = [[coder decodeObjectForKey:kWCBTextFileKey] retain];
	_lineNumber = [coder decodeIntegerForKey:kWCBLineNumberKey];
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[super encodeWithCoder:coder];
	[coder encodeConditionalObject:_textFile forKey:kWCBTextFileKey];
	[coder encodeInteger:_lineNumber forKey:kWCBLineNumberKey];
}

- (id)copyWithZone:(NSZone *)zone {
	WCBookmark *copy = [super copyWithZone:zone];
	
	copy->_textFile = [_textFile retain];
	copy->_lineNumber = _lineNumber;
	
	return self;
}

@synthesize textFile=_textFile,lineNumber=_lineNumber;

@dynamic fileAndLineNumber;
- (NSString *)fileAndLineNumber {
	return [NSString stringWithFormat:@"%@:%u", [_textFile name], _lineNumber+1];
}

- (id)initWithTextFile:(WCTextFile *)textFile lineNumber:(NSUInteger)lineNumber; {
	if (!(self = [super initWithNodeName:nil]))
		return nil;
	
	_textFile = [textFile retain]; 
	_lineNumber = lineNumber;
	
	return self;
}

+ (WCBookmark *)bookmarkWithTextFile:(WCTextFile *)textFile lineNumber:(NSUInteger)lineNumber; {
	return [[[[self class] alloc] initWithTextFile:textFile lineNumber:lineNumber] autorelease];
}
@end
